﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brouteforce.Map;
using Brouteforce.NewManagers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Brouteforce.Animation;
using Microsoft.Xna.Framework.Content;
using Brouteforce.Screens.PlayingScreen.Panels;

namespace Brouteforce.Characters {

    public struct Key {
        public string HumanOrMonster;
        public string Type;
        public string Movement;
        public string Direction;
        public Key(string p1, string p2, string p3, string p4) {
            HumanOrMonster = p1;
            Type = p2;
            Movement = p3;
            Direction = p4;
        }
    }

    public abstract class Character {
        public MapCell MapCell;
        public bool Selected = false;
        public Resources InitialResources;
        public Resources CurrentResources;
        protected LinearMovementTransition _walkingTransition;
        protected LinearFrameAnimationTransition _animationTransition;
        public bool IsHuman = false;
        public string Type = "?";
        private GamePanel _gamePanel;

        protected Character(GamePanel gamePanel, Resources initialResource, Vector2 initialPosition) {
            InitialResources = initialResource;
            _gamePanel = gamePanel;
            _walkingTransition = new LinearMovementTransition(0, initialPosition, initialPosition);
        }

        public Vector2 DrawOffset{
            get { return new Vector2(-24, -38); }
        }

        public static Dictionary<Key, List<Texture2D>> VisualMap = new Dictionary<Key, List<Texture2D>>();

        public virtual Vector2 PositionInWorld {
            get {return _walkingTransition.CurrentPosition;}
        }

        public virtual Texture2D Texture {
            get { return _animationTransition.CurrentTexture; }
        }

        public float DrawDepth { get; set; }

        

        public void UpdateTileLocation(TileMap myMap) {
            if (MapCell != null){
                MapCell.CharactersInCell.Remove(this);
            }


            MapCell cell = myMap.GetCellAtWorldPoint(PositionInWorld);
            MapCell = cell;
            cell.CharactersInCell.Add(this);
        }

        public static void LoadContent(ContentManager Content){
            List<string> HumanOrMonster = new List<string>(new string[]{"Humans", "Monsters"});
            List<string> Type = new List<string>(new string[]{"Archer", "Bomber", "Defender"});
            List<string> ArcherMovement = new List<string>(new string[]{"been hit", "running", "walking", "shooting", "talking"});
            List<string> DefenderMovement = new List<string>(new string[]{"been hit", "walking",  "attack", "talking"});
            List<string> BomberMovement = new List<string>(new string[]{"walking"});
            List<string> Direction = new List<string>(new string[]{"sw", "se", "nw", "ne"});

            foreach (var h in HumanOrMonster) {
                foreach (var t in Type) {
                    foreach (var d in Direction){
                        if (t == "Archer") {
                            foreach (var m in ArcherMovement) {
                                VisualMap.Add(new Key(h, t, m, d), new List<Texture2D>());
                            }
                        }
                        if (t == "Bomber") {
                            foreach (var m in BomberMovement) {
                                VisualMap.Add(new Key(h, t, m, d), new List<Texture2D>());
                            }
                        }
                        if (t == "Defender") {
                            foreach (var m in DefenderMovement) {
                                VisualMap.Add(new Key(h, t, m, d), new List<Texture2D>());
                            }
                        }
                    }
                }
            }



            foreach (var v in VisualMap.Keys) {
                int i = 0;
                try {
                    while (true) {
                        string fileName = "Characters/" + v.HumanOrMonster + "/" + v.Type + "/" + v.Movement + " " + v.Direction + "000" + i;
                        VisualMap[v].Add(Content.Load<Texture2D>(fileName));
                        i++;
                    }
                } catch (ContentLoadException exception) {
                    
                }
            }


 //           neRunningFrames.Add(Content.Load<Texture2D>(@"Characters/Archer/running ne000" + i));
            
        }

        public void MoveTo(Vector2 worldPosition) {
            _walkingTransition.OnTransitionDone += (() => _gamePanel.UpdateCharacterCellLocation(this));
            _walkingTransition.Duration = 1;
            _walkingTransition.StartPosition = _walkingTransition.CurrentPosition;
            _walkingTransition.EndPosition = worldPosition;
            _walkingTransition.startTransition();

            string direction;
            if (_walkingTransition.StartPosition.Y > _walkingTransition.EndPosition.Y) direction = "n";
            else direction = "s";

            if (_walkingTransition.StartPosition.X > _walkingTransition.EndPosition.X) direction += "w";
            else direction += "e";


            _animationTransition.Textures = VisualMap[new Key((IsHuman? "Humans": "Monsters"), Type, "walking", direction)];
            _animationTransition.TextureSwitchDuration = 0.045;
            _animationTransition.startTransition();
            _animationTransition.Duration = 1;
        }

    }
}
